<?php
// +----------------------------------------------------------------------
// | Azthink通用开发平台 [ TOMORROW IS ANOTHER DAY ]
// +----------------------------------------------------------------------
// | Copyright (c) 2018-2019 http://www.azthink.top All rights reserved.
// +----------------------------------------------------------------------
// | Author 李忠<ny_lizhong@126.com>
// +----------------------------------------------------------------------

namespace app\admin\model;

use think\db\Where;
use think\Model;
use app\common\model\Tree;

/**
 * 机构模型
 * @author 李忠<ny_lizhong@126.com>
 * @createAt 2018-09-06 14:59:05
 */
class Office extends Model {
    // 设置当前模型对应的完整数据表名称
    protected $table = '__SYS_OFFICE__';

    // 自动写入时间戳
    protected $autoWriteTimestamp = true;

    //数据自动完成
    protected $auto = ['update_by' => UID];
    protected $insert = ['create_by' => UID];

    //修改器
    public function getStatusTextAttr($value, $data) {
        return get_status_html($data['status']);
    }

    //修改器
    public function getTypeTextAttr($value, $data) {
        return get_config_text('office_type', $data['type']);
    }

    //自定义方法
    public static function lists($status, $type, $title, $pid) {
        //$map = array(['a.pid', '=', $pid]);

        $map = new Where();
        $map['a.pid'] = $pid;

        if ($status !== null && $status !== '') {
            $map['a.status'] = $status;
        } else {
            $map['a.status'] = ['>=', 0];
        }

        if ($title !== '' && !empty($title)) {
            $map['a.title|a.name'] = ['like', '%' . $title . '%'];
        }
        if ($type != null && !empty($type)) {
            $map['a.type'] = $type;
        }
        return Office::alias('a')->leftjoin('__SYS_OFFICE__ b', 'a.pid = b.id')->where($map)
            ->field('a.*, b.title as parent_title, (select count(1) from ' . self::getTable('sys_office') . ' c where c.pid = a.id and c.status>=0) as child_count')
            ->order('a.sort')->paginate();
    }

    public static function listAuthAll($auth_list) {
        $list = self::where('status', 1)->order('sort')->select();
        foreach ($list as &$item) {
            $item['checked'] = '';
            foreach ($auth_list as $auth_id) {
                if ($item['id'] == $auth_id) {
                    $item['checked'] = 'checked';
                    break;
                }
            }
        }
        return $list;
    }

    public static function listMy($type = null) {
        $where = new Where();
        $where['status'] = 1;
        if (!empty($type)){
            $where['type'] = $type;
        }
        if (!is_admin()) {
            $data_rules = session('data_rules');
            $where['id'] = ['in', $data_rules];
            return self::where($where)->field('id,title')->order('sort')->select();
        } else {
            return self::where($where)->field('id,title')->order('sort')->select();
        }
    }

    public static function state($id, $status) {
        $data['id'] = $id;
        $data['status'] = $status;
        return Office::update($data);
    }

    // 将Office菜单转换为树
    public static function toFormatTree($module) {
        $list = self::where(array(['status', '=', 1]))->order('sort')->field('id,pid,title')->select();

        $treeModel = new Tree();
        $tree_list = $treeModel->toFormatTree($list->toArray());
        return $tree_list;
    }

    // 将Menu菜单转换为配置下拉数据
    public function listToConfig() {
        return list_to_config($this->toFormatTree(), 'id', 'title_show');
    }

    public static function getPid($id) {
        return self::where('id', $id)->value('pid');
    }
}
